package suites.deeplink {
	import suites.AbstractConfig;

	import org.flexunit.asserts.assertEquals;
	import org.flexunit.asserts.assertTrue;
	import org.splink.deepsplink.page.params.PageParameter;

	/**
	 * @author Max Kugland
	 */
	public class DeeplinkControllerTest extends AbstractDeeplinkControllerTest {
		[Test]
		public function autoHome() : void {
			var count : uint = 0;
			var fnc : Function = function(id : String, params : Array): void {
				assertEquals(0, params.length);
				assertEquals(AbstractConfig.HOME, id);
				count++;
			};
			createCtrl(fnc);
			assertEquals(0, _ctrl.history.length);
			_ctrl.start();
			assertEquals(1, count);			assertEquals(1, _ctrl.history.length);
		}

		[Test]
		public function gotoPage() : void {
			var c : uint = 0;
			var fnc : Function = function(id : String, params : Array): void {
				assertEquals(0, params.length);
				switch(c) {
					case 0:
						assertEquals(AbstractConfig.HOME, id);
						c++;
						_ctrl.gotoPage("2");
						break;
					case 1:
						assertEquals("2", id);
				}
			};
			
			createCtrl(fnc).start();
		}

		[Test]
		public function directGotoPage() : void {
			var fnc : Function = function(id : String, params : Array): void {
				assertEquals(0, params.length);
				assertEquals("2", id);
			};
			
			createCtrl(fnc);
			_ctrl.gotoPage("2");
			_ctrl.start();
			assertEquals(1, _ctrl.history.length);
		}

		[Test]
		public function unknownId() : void {
			var unknown : String = "some-unknown-id";
			var fnc : Function = function(id : String, params : Array): void {
				assertEquals(0, params.length);
				assertEquals(unknown, id);
			};
			createCtrl(fnc);
			_ctrl.gotoPage(unknown);
			_ctrl.start();
			assertEquals(1, _ctrl.history.length);
		}
		
		[Test]
		public function gotoSingleParam() : void {
			var fnc : Function = function(id : String, params : Array): void {
				assertEquals(1, params.length);
				assertEquals("2", id);
				assertTrue(params[0] is PageParameter);
				assertEquals("p-1", PageParameter(params[0]).value);
				assertEquals("param1", PageParameter(params[0]).key);
			};
			createCtrl(fnc);
			_ctrl.gotoPage("2", new Array(new PageParameter("param1", "p-1")));
			_ctrl.start();

			assertEquals(1, _ctrl.history.length);
			assertEquals("2?param1=p-1", _ctrl.history[0]);
		}

		[Test]
		public function gotoMultiParams() : void {
			var fnc : Function = function(id : String, params : Array): void {
				assertEquals(2, params.length);
				assertEquals("2", id);
				assertTrue(params[0] is PageParameter);
				assertTrue(params[1] is PageParameter);
				assertEquals("p-1", PageParameter(params[0]).value);
				assertEquals("param1", PageParameter(params[0]).key);
				assertEquals("p-2", PageParameter(params[1]).value);
				assertEquals("param2", PageParameter(params[1]).key);
			};
			createCtrl(fnc);
			_ctrl.gotoPage("2", new Array(new PageParameter("param1", "p-1"), new PageParameter("param2", "p-2")));
			_ctrl.start();
			
			assertEquals(1, _ctrl.history.length);
			assertEquals("2?param1=p-1&param2=p-2", _ctrl.history[0]);
		}
	}
}
